/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn2;

/**
 *
 * @author michaellangmayr
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        int listenPort = 4321;
        Overlay myOverlay = null;
        
        

        if (args.length > 0) {
            listenPort = Integer.parseInt(args[1]);

            

            System.out.println("[Main] Starting peer...");

            if (args.length == 4) {
                // we connect to an already existing network

                String firstKnownPeerAdress = args[2];
                int firstKnownPeerPort = Integer.parseInt(args[3]);

                RemotePeer firstKnownPeer = new RemotePeer(firstKnownPeerAdress, firstKnownPeerPort);
                myOverlay = new Overlay(listenPort, firstKnownPeer);

            } else {
                // we are the first peer in a new network
                System.out.println("[Main] we are the first peer in a new network...");
                myOverlay = new Overlay(listenPort);
            }

            // Setting Path to local File Repository
            myOverlay.setRepoPath(args[0]);

            System.out.println("[Main] Starting listener thread...");
            ListenerThread listenThread = new ListenerThread(listenPort, myOverlay);
            listenThread.start();

            if(myOverlay != null){
                ViewPeer vp = new ViewPeer(myOverlay);
                vp.setVisible(true);
            }


        } else {
            System.out.println("[Main] missing parameters!");
            return;
        }


        
        

    }

}
